home *** CD-ROM | disk | FTP | other *** search
- /* File : strtod.c
- Author : Based on str2dbl.c by Richard A. O'Keefe @ Quintus Computer Systems, Inc.
- Updated: 12-11-93 by Jonathan Kimmitt
- Defines: double strtod(char *str, char**ptr)
- */
-
- /* This is an implementation of the strtod() function based on str2dbl()
- by the above author but with dependence on external libraries removed,
- and certain simplifications.
-
- From the author:
-
- There are two reasons why this should be provided to the net:
- (a) some UNIX systems do not yet have strtod(), or do not have it
- available in the BSD "universe" (but they do have atof()).
- (b) some of the UNIX systems that *do* have it get it wrong.
- (some crash with large arguments, some assign the wrong *ptr value).
- There is a reason why *we* are providing it: we need a correct version
- of strtod(), and if we give this one away maybe someone will look for
- mistakes in it and fix them for us (:-).
- */
-
- /* The following constants are machine-specific. MD{MIN,MAX}EXPT are
- integers and MD{MIN,MAX}FRAC are strings such that
- 0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
- 0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
- MD{MIN,MAX}FRAC must not have any trailing zeros.
- The values here are for IEEE-754 64-bit floats.
- It is not perfectly clear to me whether an IEEE infinity should be
- returned for overflow, nor what a portable way of writing one is,
- so HUGE is just 0.MAXFRAC*10**MAXEXPT (this seems still to be the
- UNIX convention).
-
- I do know about <values.h>, but the whole point of this file is that
- we can't always trust that stuff to be there or to be correct.
- */
-
- #include <errno.h>
- #include <stdlib.h>
- #include <math.h>
-
- short __D_INF[] = { 0x7FF0, 0x0000, 0x0000, 0x0000 };
-
- #undef HUGE_VAL
- #define HUGE_VAL (*((double *) __D_INF))
-
- #define MDMINEXPT -323
- #define ZERO 0.0
- #define MDMAXEXPT 309
- #define MDMINFRAC 0.494065645841246544
- #define MDMAXFRAC 0.17976931348623147
-
- extern int errno;
-
- #if __STDC__
- double strtod (const char *sp, char **ptr)
- #else
- double strtod(sp, ptr)
- const char *sp;
- char **ptr;
- #endif
- {
- int sign = 1;
- int scale = 0;
- int dotseen = 0;
- int esign = 1;
- int expt = 0;
- const char *save;
- register int c;
- double fp_tot = 0.0;
- double fp_scale = 1.0;
- if (ptr) *ptr = (char *)sp;
- while (*sp == ' ') sp++;
- if (*sp == '-') sign -= 2, sp++;
- for (save = sp; c = *sp; sp++)
- if (c == '.') {
- if (dotseen) break;
- dotseen++;
- } else
- if ((unsigned)(c-'0') > (unsigned)('9'-'0')) {
- break;
- } else
- if ((c == '0') && (fp_tot == 0.0)) {
- /* No non-zero digits seen yet */
- /* If a . has been seen, scale must be adjusted */
- if (dotseen) scale--;
- } else {
- /* This is a non leading zero digit, so we want to keep it */
- fp_tot += (fp_scale *= 0.1)*(c-'0');
- /* If it precedes a ., scale must be adjusted */
- if (!dotseen) scale++;
- }
- if (sp == save) {
- errno = EDOM; /* what should this be? */
- return ZERO;
- }
-
- save = sp;
- do {
- c = *sp++;
- if (c != 'e' && c != 'E') break;
- c = *sp++;
- if (c == '-') esign -= 2, c = *sp++; else
- if (c == '+' || c == ' ') c = *sp++;
- if ((unsigned)(c-'0') > (unsigned)('9'-'0')) break;
- while (c == '0') c = *sp++;
- for (; (unsigned)(c-'0') <= (unsigned)('9'-'0'); c = *sp++)
- expt = expt*10 + c-'0';
- if (esign < 0) expt = -expt;
- save = sp-1;
- } while (0);
- if (ptr) *ptr = (char *)save;
- expt += scale;
- /* Now the number is sign*0.fraction*10**expt */
- errno = ERANGE;
- if (expt > MDMAXEXPT) {
- return HUGE*sign;
- } else
- if (expt == MDMAXEXPT) {
- if (fp_tot > MDMAXFRAC) return HUGE*sign;
- } else
- if (expt < MDMINEXPT) {
- return ZERO*sign;
- } else
- if (expt == MDMINEXPT) {
- if (fp_tot < MDMINFRAC) return ZERO*sign;
- }
- /* We have now established that the number can be */
- /* represented without overflow or underflow */
- if (expt < 0)
- {
- expt = -expt;
- fp_scale = 0.1;
- }
- else
- fp_scale = 10.0;
- while (expt)
- {
- if (expt&1) fp_tot *= fp_scale;
- expt >>= 1;
- fp_scale *= fp_scale;
- }
- errno = 0;
- return fp_tot*sign;
- }
-
-